<template>
  <el-dialog
    :title="!dataForm.reId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="reUserId">
      <el-input v-model="dataForm.reUserId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="阅读数量" prop="reBook">
      <el-input v-model="dataForm.reBook" placeholder="阅读数量"></el-input>
    </el-form-item>
    <el-form-item label="阅读时间" prop="reTime">
      <el-input v-model="dataForm.reTime" placeholder="阅读时间"></el-input>
    </el-form-item>
    <el-form-item label="关注" prop="reAttention">
      <el-input v-model="dataForm.reAttention" placeholder="关注"></el-input>
    </el-form-item>
    <el-form-item label="粉丝" prop="reFans">
      <el-input v-model="dataForm.reFans" placeholder="粉丝"></el-input>
    </el-form-item>
    <el-form-item label="获得的点攒" prop="reUpvote">
      <el-input v-model="dataForm.reUpvote" placeholder="获得的点攒"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          reId: 0,
          reUserId: '',
          reBook: '',
          reTime: '',
          reAttention: '',
          reFans: '',
          reUpvote: ''
        },
        dataRule: {
          reUserId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          reBook: [
            { required: true, message: '阅读数量不能为空', trigger: 'blur' }
          ],
          reTime: [
            { required: true, message: '阅读时间不能为空', trigger: 'blur' }
          ],
          reAttention: [
            { required: true, message: '关注不能为空', trigger: 'blur' }
          ],
          reFans: [
            { required: true, message: '粉丝不能为空', trigger: 'blur' }
          ],
          reUpvote: [
            { required: true, message: '获得的点攒不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.reId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.reId) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbread/info/${this.dataForm.reId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.reUserId = data.tbRead.reUserId
                this.dataForm.reBook = data.tbRead.reBook
                this.dataForm.reTime = data.tbRead.reTime
                this.dataForm.reAttention = data.tbRead.reAttention
                this.dataForm.reFans = data.tbRead.reFans
                this.dataForm.reUpvote = data.tbRead.reUpvote
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbread/${!this.dataForm.reId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'reId': this.dataForm.reId || undefined,
                'reUserId': this.dataForm.reUserId,
                'reBook': this.dataForm.reBook,
                'reTime': this.dataForm.reTime,
                'reAttention': this.dataForm.reAttention,
                'reFans': this.dataForm.reFans,
                'reUpvote': this.dataForm.reUpvote
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
